package com.sdp.th.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.entity.SysEmp;
import com.sdp.th.db.th_eq_info.service.IThEqInfoService;
import com.sdp.th.db.th_eq_repair.service.IThEqRepairService;
import com.sdp.th.entity.ThEqInfo;
import com.sdp.th.entity.ThEqRepair;
import com.sdp.th.utils.CurrentLoginEmp;
import com.sdp.th.utils.ThConstants;

/**
 * <p>
 * 设备报修维修信息 前端控制器
 * </p>
 *
 * @author zsp
 * @since 2025-04-18
 */
@RestController
@RequestMapping("/ThEqRepairController")
public class ThEqRepairController {

	@Autowired
	private IThEqRepairService iThEqRepairService;
	
	@Autowired
	private Query query;
	
	@Autowired
	private CurrentLoginEmp CurrentLoginEmp;
	
	@Autowired
	private IThEqInfoService iThEqInfoService;

	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iThEqRepairService, ThEqRepair.class, dataCenter));
	}
	
	
	@PostMapping(value = "/repair")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> repair(@RequestBody ThEqRepair repair) {
		if(repair==null) {
			return Result.error("报修信息为空");
		}
		if(StringUtils.isEmpty(repair.getDeviceNo())) {
			return Result.error("设备信息为空");
		}
		if(StringUtils.isEmpty(repair.getKeyId())) {
			ThEqInfo eqInfo = iThEqInfoService.queryEqInfoByNo(repair.getDeviceNo());
			SysEmp emp = CurrentLoginEmp.getCurrentLoginInfo();
			repair.setRepairTime(LocalDateTime.now());
			repair.setRepairEmoNo(emp.getEmpNo());
			repair.setRepairEmpName(emp.getEmpName());
			repair.setRepairState(ThConstants.设备维修状态_报修.getCode());
			repair.setDeviceType(eqInfo.getDeviceType());
			repair.setDeviceName(eqInfo.getDeviceName());
			repair.setDeviceNo(eqInfo.getDeviceNo());
			repair.setDeviceRoom(eqInfo.getDeviceRoom());
			repair.setDeviceRegion(eqInfo.getDeviceRegion());
			iThEqRepairService.save(repair);
			
			//报修时将设备状态设置为故障
			eqInfo.setDeviceState(ThConstants.设备状态_故障.getCode());
			iThEqInfoService.updateById(eqInfo);
			
		}else {
			iThEqRepairService.updateById(repair);
		}
		return Result.ok();
	}
	
	@PostMapping(value = "/maintain")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> maintain(@RequestBody ThEqRepair repair) {
		if(repair==null) {
			return Result.error("维修信息为空");
		}
		if(StringUtils.isEmpty(repair.getKeyId())) {
			return Result.error("维修数据不完整");
		}
		ThEqRepair dbRepair = iThEqRepairService.getById(repair.getKeyId());
		if(dbRepair==null) {
			return Result.error("此报修数据已不存在");
		}
		if(ThConstants.设备维修状态_维修.getCode().equals(repair.getRepairState())) {
			return Result.error("此报修单已维修过");
		}
		
		SysEmp emp = CurrentLoginEmp.getCurrentLoginInfo();
		dbRepair.setMaintainTime(LocalDateTime.now());
		dbRepair.setMaintainEmpNo(emp.getEmpNo());
		dbRepair.setMaintainEmpName(emp.getEmpName());
		dbRepair.setRepairState(ThConstants.设备维修状态_维修.getCode());
		iThEqRepairService.updateById(dbRepair);
		
		ThEqInfo eqInfo = iThEqInfoService.queryEqInfoByNo(repair.getDeviceNo());
		//维修后将设备状态设置为正常
		eqInfo.setDeviceState(ThConstants.设备状态_正常.getCode());
		iThEqInfoService.updateById(eqInfo);
		
		return Result.ok();
	}
}

